﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>选择部门</title>
    <link href="../../../Comm/Style/CommStyle.css" rel="stylesheet" type="text/css" />
    <link href="../../../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <link href="../../../Scripts/easyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../../Scripts/easyUI/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/easyUI/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/QueryString.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../../Scripts/config.js"></script>
    <script type="text/javascript" src="../../../Comm/Gener.js"></script>
    <script src="../../../Scripts/EasyUIUtility.js" type="text/javascript"></script>
    <base target="_self" />
    <script type="text/javascript">

        //页面启动函数.
        $(function () {
            $("#Msg").html("<img src=../../../Img/loading.gif />&nbsp;正在加载,请稍后......");
            //求出来已经设置的延续子流程.
            var nodeID = GetQueryString("FK_Node");
            var orgNo = GetQueryString("OrgNo");

            //global
            var global = window;
            global.selectedNodes = [];
            global.selectedRows = [];

            //跟节点编号.
            var rootNo = "0";

            var webUser = new WebUser();
            if (orgNo == null)
                orgNo = webUser.OrgNo;

            rootNo = orgNo;  

            //多对多实体.
            var nodeDepts = new Entities("BP.WF.Template.NodeDepts");
            nodeDepts.Retrieve("FK_Node", nodeID);

            var nodeDept = new Entity("BP.WF.Template.NodeDept");

            $.each(nodeDepts, function (i, o) {

                var enDot = new Entity(nodeDept, o);

                var no = enDot.FK_Dept;
                var name = enDot.FK_DeptText;
                global.selectedNodes.push({
                    "No": no,
                    "Name": name
                }); 
            });

            loadTree(rootNo, global, orgNo, nodeID);

            $("#Msg").html("");


        });

        function loadTree(rootNo, global, orgNo, nodeID) {


            var ens = new Entities("BP.Port.Depts");
            ens.Retrieve("OrgNo", orgNo);
            var jsonTree = createJsonToTree(ens, rootNo);

            //查询出来数据.
            $('#tree').tree({
                data: jsonTree,         // 数据源
                checkbox: true,         //是否显示复选框
                highlightSelected: false,    //是否高亮选中
                animate: true,
                onSelected: function (node) {

                    var tree = $('#tree');
                    if (node.state.open) {
                        //处于展开状态则折叠
                        tree.tree('collapseNode', node.nodeId);
                    } else {
                        //展开
                        tree.tree('expandNode', node.nodeId);
                    }
                }
            });
        }

        //树形结构转换
        var treeJson = [];
        function createJsonToTree(jsonArray, parentNo) {
            if (parentNo != "0") {
                //增加当前用户所在的部门
                var en = new Entity("BP.Port.Dept", parentNo);
                var checked = false;
                var selectedNodes = window.selectedNodes;
                var sel = $.grep(selectedNodes, function (obj) {
                    return en.No == obj.No;
                });

                if (sel.length > 0) {
                    checked = true;
                }
                treeJson.push({
                    "id": en.No,
                    "text": en.Name,
                    "children": creatTreeChildern(jsonArray, en.No),
                    "checked": checked,
                    "state": 'closed'
                });
            } else {
                 for (var i = 0; i < jsonArray.length; i++) {
                var data = jsonArray[i];
                if (data.ParentNo == parentNo) {
                    var checked = false;
                    var selectedNodes = window.selectedNodes;
                    var sel = $.grep(selectedNodes, function (obj) {
                        return data.No == obj.No;
                    });

                    if (sel.length > 0) {
                        checked = true;
                    }
                    treeJson.push({
                        "id": data.No,
                        "text": data.Name,
                        "children": creatTreeChildern(jsonArray, data.No),
                        "checked": checked,
                        "state": 'closed'
                    });
                }
            }
            }

           
            function _(treeArray) {
                $.each(treeArray, function (i, o) {
                    if ($.isArray(o.nodes)) {
                        if (o.nodes.length == 0) {
                            o.nodes = undefined;
                        } else {
                            _(o.nodes);
                        }
                    }
                });
            }
            _(treeJson);
            return treeJson;
        }

        function creatTreeChildern(jsonArray, parentNo) {
            var childList = []
            for (var i = 0; i < jsonArray.length; i++) {
                var data = jsonArray[i];
                if (data.ParentNo == parentNo) {
                    var checked = false;
                    var selectedNodes = window.selectedNodes;
                    var sel = $.grep(selectedNodes, function (obj) {
                        return data.No == obj.No;
                    });

                    if (sel.length > 0) {
                        checked = true;
                    }
                    childList.push({
                        "id": data.No,
                        "text": data.Name,
                        "children": creatTreeChildern(jsonArray, data.No),
                        "checked": checked,
                        "state": 'closed'
                    });
                }
            }

            return childList;
        }


        //保存.
        function Save() {

            var nodeID = GetQueryString("FK_Node");
            var orgNo = GetQueryString("OrgNo");

            //删除,该组织下已经保存的岗位. 
            //var sql = "DELETE FROM WF_NodeDept WHERE FK_Node=" + nodeID + " AND FK_Dept IN (SELECT No FROM Port_Dept WHERE OrgNo='" + orgNo + "')";
           // DBAccess.RunSQL(sql);
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_Admin_AttrNode");
            handler.AddPara("nodeID", nodeID);
            handler.AddPara("orgNo", orgNo);
            handler.DoMethodReturnString("NodeDeptGroup_Dele");

            //获取复选框选中的节点
            var nodes = $('#tree').tree('getChecked');
            var s = '';
            for (var i = 0; i < nodes.length; i++) {
                var en = new Entity("BP.WF.Template.NodeDept");
                en.FK_Node = nodeID;
                en.FK_Dept = nodes[i].id;
                en.Insert();

            }


            var node = new Entity("BP.WF.Template.NodeExt", nodeID);
            node.DeliveryWay = 4;
            node.Update();

            parent.location.reload(); 
            //self.opener.location.reload(); 
            //window.opener.document.location.reload();
        }

    </script>
</head>
<body>
    <input type="button" value='保存' onclick='Save();' id='save' />
    <div id="tree">
    </div>
    <div id="Msg">
    </div>
</body>
</html>
